package info.androidhive.actionbar;


import info.androidhive.actionbar.layout.Layout1;
import info.androidhive.actionbar.menuactivites.EventApplyActivity;
import info.androidhive.actionbar.menuactivites.MyInfoActivity;
import info.androidhive.actionbar.menuactivites.NoticeActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SearchView;


import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SlidingMenuMain extends ActionBarActivity implements
								ActionBar.OnNavigationListener{

	MainLayout mLayout;
	private ListView lvMenu;
	private String[] lvMenuItems;
	Button btMenu;
	TextView tvTitle;
	int currentTV;
	
	private android.app.ActionBar actionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    
		//getActionBar().setHomeButtonEnabled(true);
		//getActionBar().setIcon(R.drawable.ic_action_select_all);
		
//		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
//	    getActionBar().hide();

		mLayout = (MainLayout) this.getLayoutInflater().inflate(
				R.layout.activity_slide, null);
		setContentView(mLayout);
		
		actionBar = getActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		

		lvMenuItems = getResources().getStringArray(R.array.menu_items);
		lvMenu = (ListView) findViewById(R.id.menu_listview);
		lvMenu.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, lvMenuItems));
		lvMenu.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				onMenuItemClick(parent, view, position, id);
			}

		});

//		btMenu = (Button) findViewById(R.id.button_menu);
//		btMenu.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				// Show/hide the menu
//				toggleMenu(v);
//			}
//		});

		currentTV = 0;
		FragmentManager fm = SlidingMenuMain.this.getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		Layout1 fragment = new Layout1();
		ft.add(R.id.activity_main_content_fragment, fragment);
		ft.commit();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
			
		MenuItem menuItem = menu.findItem(R.id.action_search);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		
		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
		
		if(searchView == null)
		{
			finish();
		}
		else
		{
			searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
		}
		
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

	 
	  if (item.getItemId() == android.R.id.home) {
	    // Show/hide the menu
	    toggleMenu(item);
	  }
	  else if(item.getItemId() == R.id.action_search)
	  {
		  
	  }
	  else if(item.getItemId() == R.id.action_location_found)
	  {
		  LocationFound();
		  
	  }
	  else if(item.getItemId() == R.id.action_refresh)
	  {
		  LocationSelection();
		  
	  }
	  else if(item.getItemId() == R.id.action_check_updates)
	  {
		  
	  }
	  else
	  {
		  
	  }
	return false;
		  
	}
	
	private void LocationFound() {
		Intent i = new Intent(SlidingMenuMain.this, LocationFound.class);
		startActivity(i);
	}
	private void LocationSelection() {
		Intent j = new Intent(SlidingMenuMain.this, LocationSelection.class);
		startActivity(j);
	}
	
	
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		// TODO Auto-generated method stub
		return false;
	}

	public void toggleMenu(MenuItem item) {
		mLayout.toggleMenu();
	}

	private void onMenuItemClick(AdapterView<?> parent, View view,
	         int position, long id) {

	      Intent intent = null;
	      if (position == 0) {
	    	  intent = new Intent(this, NoticeActivity.class);
	      } else if (position == 1) {
	    	  intent = new Intent(this, MyInfoActivity.class);
	      }
	      else if(position == 2)
	      {
	    	  intent = new Intent(this, EventApplyActivity.class);
	      }

	      if (intent != null) {
	         startActivity(intent);
	         currentTV = position;
	      }
	      mLayout.toggleMenu();
	}

	@Override
	public void onBackPressed() {
		if (mLayout.isMenuShown()) {
			mLayout.toggleMenu();
		} else {
			super.onBackPressed();
		}
	}
}
